<template>
  <div class="box">
    <div class="card-container">
      <el-row :gutter="10">
      <el-form :model="queryParams" ref="queryForm" label-width="90px" :inline="true" v-show="showSearch">
        <el-col :span="19" :offset="0">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" :style="'width:215px'" placeholder="请输入项目名称" clearable
              size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="设备序列号" prop="serialNo">
            <el-input v-model="queryParams.serialNo" :style="'width:215px'" placeholder="请输入设备序列号" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="在线状态" prop="onlineStatus">
            <el-select v-model="queryParams.onlineStatus" :style="'width:215px'" placeholder="请选择状态" clearable
              size="small">
              <el-option v-for="dict in dict.type.sys_on_line_status" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="告警类型" prop="alarmDesc">
            <el-select v-model="queryParams.alarmDesc" :style="'width:215px'" clearable placeholder="请选择告警类型">
              <el-option value="NTP对时不成功">
                NTP对时不成功
              </el-option>
              <el-option value="MQTT连接不成功">
                MQTT连接不成功
              </el-option>
              <el-option value="文件服务状态异常">
                文件服务状态异常
              </el-option>
              <el-option value="自动定位服务状态异常">
                自动定位服务状态异常
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item label="告警时间" prop="alarmTime">
            <el-date-picker clearable size="small" :style="'width:215px'" v-model="queryParams.alarmTime"
              range-separator="-" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="处理时间" prop="updateTime">
            <el-date-picker clearable size="small" :style="'width:215px'" v-model="queryParams.updateTime"
              range-separator="-" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>

        </el-col>
        <el-col :span="5" :offset="0">
          <el-form-item>
            <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    </div>
   

    <div class="card-container">
      <div class="body-title">
        <div class="body-title-left">
          告警记录
          <div class="header-button">
            <div @click="queryButton('all')" :class="queryBut === 'all' ? 'true' : ''">全部</div>
            <div @click="queryButton('pending')" :class="queryBut === 'pending' ? 'true' : ''">待处理
            </div>
          </div>
        </div>
        <div class="body-title-right">
          <!-- <el-button  size="mini" :disabled="multiple" v-show="!multiple"
            @click="handleDelete">删除</el-button> -->
          <!-- <el-button  size="mini"  @click="handleUpdate('处理')"
            :disabled="ids.length > 1" v-show="!multiple">
            处理
          </el-button> -->
          <el-button  size="mini" v-hasPermi="['alarmConfig:add']" @click="handleWarningOpen">告警配置</el-button>
          <!-- <el-button  size="mini" v-show="!multiple" @click="handleExport">导出</el-button> -->
        </div>
      </div>
      <el-table v-loading="loading" height="420" :data="warningList">
        <!-- @selection-change="handleSelectionChange" :row-key="getRowKeys" -->
        <!-- <el-table-column type="selection" width="55" :reserve-selection="true" align="center" /> -->
        <el-table-column label="告警时间" show-overflow-tooltip align="center" prop="alarmTime" width="180">
          <!-- <template slot-scope="scope">
            <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d}') }}</span>
          </template> -->
        </el-table-column>
        <el-table-column label="项目名称" show-overflow-tooltip align="center" prop="projectName" />
        <el-table-column label="设备序列号" show-overflow-tooltip align="center" prop="serialNo" />
        <el-table-column label="在线状态" align="center" prop="onlineStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_on_line_status" :value="scope.row.onlineStatus" />
          </template>
        </el-table-column>
        <el-table-column label="告警类型" show-overflow-tooltip align="center" prop="alarmDesc" />
        <!-- <el-table-column label="告警类型" show-overflow-tooltip align="center" prop="alarmTypeLabel" /> -->
        <el-table-column label="处理状态" show-overflow-tooltip align="center" prop="handleStateLabel" />
        <el-table-column label="处理时间" show-overflow-tooltip align="center" prop="updateTime" />
        <el-table-column label="处理人" show-overflow-tooltip align="center" prop="handleBy" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" v-if="scope.row.handleState == '0'" v-hasPermi="['alarmLog:edit']"
              @click="handleUpdate('处理', scope.row)">
              处理</el-button>
            <el-button size="mini" type="text" v-else v-hasPermi="['alarmLog:query']"
              @click="handleUpdate('查看', scope.row)">
              查看</el-button>
            <!-- <el-button size="mini" type="text" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['alarmLog:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 处理 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
      <div class="card-container">
        <el-row :gutter="10">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="24" :offset="0">
            <el-form-item label="是否误报" prop="isFalseAlarm">
              <el-radio-group v-if="title !== '查看'" v-model="form.isFalseAlarm">
                <el-radio v-for="item in this.dict.type.sys_yes_no" :key="item.value" :label="item.value">{{ item.label
                }}</el-radio>
              </el-radio-group>
              <span v-else>{{ form.isFalseAlarmLabel }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <el-form-item label="处理结果" prop="handleResult">
              <el-input v-if="title !== '查看'" v-model="form.handleResult" type="textarea" maxlength="100" show-word-limit
                placeholder="请输入,最多100字" />
              <span v-else>{{ form.handleResult }}</span>
            </el-form-item>
          </el-col>
          <el-col v-if="title === '查看'" :span="24" :offset="0">
            <el-form-item label="处理时间" prop="updateTime">
              <span>{{ form.updateTime }}</span>
            </el-form-item>
          </el-col>
          <el-col v-if="title === '查看'" :span="24" :offset="0">
            <el-form-item label="处理人" prop="">
              <span>{{ form.handleBy }}</span>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      </div>
    
      <div slot="footer" v-if="title !== '查看'" class="dialog-footer">
        <el-button  @click="submitForm()">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 告警配置 -->
    <el-dialog :title="'告警配置-预警终端'" :visible.sync="warningOpen" width="500px" append-to-body>
      <div class="card-container">
        <el-row :gutter="10">
        <el-form ref="warningForm" :model="warningForm" :rules="warningRules" label-width="80px">
          <el-col :span="24" :offset="0">
            <div class="warning-title">设备离线告警</div>
            <el-form-item label="沉默周期" prop="offlineConfig">
              <el-input :style="'width:100px;'" type="number" v-model.number="warningData.offlineConfig"
                placeholder="24" />
              小时
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <div class="warning-title">NTP对时不成功告警</div>
            <el-form-item label="沉默周期" prop="ntpConfig">
              <el-input :style="'width:100px;'" type="number" v-model.number="warningData.ntpConfig" placeholder="24" />
              小时
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24" :offset="0">
            <div class="warning-title">磁盘使用率超过阈值告警</div>
            <el-form-item label="沉默周期" prop="">
              <el-input :style="'width:50px;'" v-model="form.code" placeholder="24" />
              小时
            </el-form-item>
          </el-col> -->
          <el-col :span="24" :offset="0">
            <div class="warning-title">MQTT连接不成功告警</div>
            <el-form-item label="沉默周期" prop="mqttConfig">
              <el-input :style="'width:100px;'" type="number" v-model.number="warningData.mqttConfig" placeholder="24" />
              小时
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <div class="warning-title">文件服务连接不成功告警</div>
            <el-form-item label="沉默周期" prop="fileConfig">
              <el-input :style="'width:100px;'" type="number" v-model.number="warningData.fileConfig" placeholder="24" />
              小时
            </el-form-item>
          </el-col>
          <el-col :span="24" :offset="0">
            <div class="warning-title">gps服务连接不成功告警</div>
            <el-form-item label="沉默周期" prop="gpsConfig">
              <el-input :style="'width:100px;'" type="number" v-model.number="warningData.gpsConfig" placeholder="24" />
              小时
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="submitWarningForm()">提 交</el-button>
        <el-button @click="warningCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLog,
  getLog,
  addLog,
  updateLog,
  delLog,
  getAlarmConfig,
  addAlarmConfig,
  upAlarmConfig,
} from '@/api/warningLog/warningLog';

export default {
  name: 'warningLog',
  dicts: ['sys_yes_no', 'sys_warning_type', "sys_on_line_status"],
  data() {
    return {
      warningOpen: false,
      warningForm: {},
      queryBut: 'all',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接入设备表格数据
      warningList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        serialNo: null,
        alarmType: null,
        alarmTime: null,
        updateTime: null,
        handleState: null,
        alarmDesc: null,
        onlineStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
      },
      warningRules: {},
      warningData: {
        offlineConfig: null,
        ntpConfig: null,
        mqttConfig: null,
        fileConfig: null,
        gpsConfig: null
      },
      submitFlag: false,
    };
  },
  created() {
    if (this.$route.query.onlineStatus) {
      this.queryParams.onlineStatus = this.$route.query.onlineStatus
      this.queryButton('pending')
    } else {
      this.getList();
    }

  },
  activated() {
    this.getList();
  },
  methods: {
    getRowKeys(row) {
      return row.alarmId;
    },
    async handleWarningOpen() {
      const res = await getAlarmConfig();
      if (res.code == 200 && res.rows.length > 0) {
        this.warningData.offlineConfig = res.rows[0].offlineConfig;
        this.warningData.ntpConfig = res.rows[0].ntpConfig;
        this.warningData.mqttConfig = res.rows[0].mqttConfig;
        this.warningData.alarmId = res.rows[0].alarmId;
        this.warningData.fileConfig = res.rows[0].fileConfig;
        this.warningData.gpsConfig = res.rows[0].gpsConfig;
        this.submitFlag = true;
      }
      this.warningOpen = true;
    },
    async submitWarningForm() {
      if (this.submitFlag) {
        // 修改
        const res = await upAlarmConfig(this.warningData);
        if (res.code == 200) {
          this.warningOpen = false;
        }
      } else {
        // 新增
        const res = await addAlarmConfig(this.warningData);
        if (res.code == 200) {
          this.warningOpen = false;
        }
      }
    },
    warningCancel() {
      this.warningOpen = false;
      this.resetForm('warningForm');
    },
    queryButton(type) {
      this.queryBut = type;
      if (this.queryBut !== 'all') {
        this.queryParams.handleState = '0';
      } else {
        this.queryParams.handleState = null;
      }
      this.handleQuery();
    },
    handleParam(row) {
      this.paramOpen = true;
      this.paramList = row;
    },
    cancelParam() {
      this.paramOpen = false;
      // this.reset();
      this.$refs['paramList'].reset();
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then((response) => {
        this.warningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alarmId: null,
        handleResult: null,
        isFalseAlarm: 'N',
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.alarmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = '添加设备管理';
    // },
    /** 修改按钮操作 */
    handleUpdate(title, row) {
      this.reset();
      // const alarmId = row.alarmId || this.ids;
      const alarmId = row && row.alarmId ? row.alarmId : this.ids[0];
      if (title === '查看') {
        getLog(alarmId).then((response) => {
          this.form = response.data;
          this.open = true;
        });
      } else {
        this.form.alarmId = alarmId;
        this.open = true;
      }
      this.title = title;
    },
    /** 提交按钮 */
    submitForm(type) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.alarmId != null) {
            this.form.checkStatus = type;
            updateLog(this.form).then((response) => {
              this.$modal.msgSuccess('提交成功');
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal
        .confirm('是否确认删除设备管理编号为"' + deviceIds + '"的数据项？')
        .then(function () {
          return delLog(deviceIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'device/device/export',
        {
          ...this.queryParams,
        },
        `device_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  padding: 20px;
}

::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

::v-deep input[type='number'] {
  -moz-appearance: textfield !important;
  // appearance: textfield !important;
}

.header-button {
  display: flex;
  align-items: center;
  margin-left: 30px;

  >div {
    width: 80px;
    height: 28px;
    text-align: center;
    margin-bottom: 0px;
    line-height: 28px;
    cursor: pointer;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    font-size: 14px;
    font-weight: 400;
  }

  >div:nth-child(1) {
    border-right-width: 0px;
    border-top-left-radius: 3px;
    border-bottom-left-radius: 3px;
  }

  >div:nth-child(2) {
    border-left-width: 0px;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
  }

  .true {
    border: 1px solid #409eff !important;
    color: #409eff;
  }
}

.body-title {
  display: flex;
  position: relative;
  // justify-content: space-between;
  align-items: center;
}

.body-title-left {
  display: flex;
  height: 100%;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #fff;
  line-height: 64px;
}

.body-title-right {
  position: absolute;
  right: 0;
}

.warning-title {
  font-weight: bold;
  font-size: 16px;
  padding-bottom: 20px;
}
</style>